package bzapps.weynak;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;
import bzapps.weynak.fb.FbSync;
 
public class Settings extends PreferenceActivity {
	 
	private static SharedPreferences pref;
	ProgressDialog pd;
	public static Handler handler;
	 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.preferences);
                pref = PreferenceManager.getDefaultSharedPreferences(this);
                                
                // Get the custom preference
                final Preference batt_levels = (Preference)findPreference("weynak.power_saving_levels");
                findPreference("weynak.power_saving.mode").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
					
					@Override
					public boolean onPreferenceChange(Preference arg0, Object arg1) {
						batt_levels.setEnabled((Boolean) arg1);
						if(!(Boolean)arg1) 
							connectToBatteryService(Settings.this,false);
						return true;
					}
				});
                findPreference("syncfb").setOnPreferenceClickListener(new OnPreferenceClickListener() {
					
					@Override
					public boolean onPreferenceClick(Preference arg0) {
						pd = ProgressDialog.show(Settings.this, "", "Synchronising with Facebook ... ",true,false);
						Thread thread = new Thread(){
							 public void run() {
								 FbSync fb = new FbSync(Settings.this);
							
									}
						};
		                thread.start();
						return true;
					}
				});
                
                handler = new Handler() {
                    public void handleMessage(Message msg) {
                   	 switch(msg.what){
                   	 case 0 :
                   		 pd.dismiss();
                   		 Toast.makeText(getBaseContext(), "Your account has been successfully synchronised!", 10).show();
                   	
                    }
                 }
            };
                
                Preference customPref = (Preference) findPreference("aboutus");
                customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
 
                                        public boolean onPreferenceClick(Preference preference) {
                                        		
                                        		startActivity(new Intent(Settings.this,Aboutus.class));
                                                return true;
                                        }
 
                                });
                findPreference("logout").setOnPreferenceClickListener(new OnPreferenceClickListener() {
					
					@Override
					public boolean onPreferenceClick(Preference arg0) {
						pref.edit().putString("uid", "0").commit();
						finish();
						Intent i = new Intent(getBaseContext(), Splash.class);
			            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			            startActivity(i);
						return true;
					}
				});
			
                findPreference("phonenumber").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
					
					@Override
					public boolean onPreferenceChange(Preference arg0, Object arg1) {
						arg0.setSummary(arg1.toString());
						return true;
					}
				});
                
                findPreference("user_status").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
					
					@Override
					public boolean onPreferenceChange(Preference arg0, Object arg1) {
						arg0.setSummary(arg1.toString());
						return true;
					}
				});
                
                findPreference("contactus").setOnPreferenceClickListener(new OnPreferenceClickListener() {
					
					@Override
					public boolean onPreferenceClick(Preference arg0) {
						startActivity(new Intent(getBaseContext(),Contactus.class));
						return true;
					}
				});
                
                /* if private mode is enabled we disallow auto update */
                final boolean isPrivate = pref.getBoolean("privacycheck", false);
                final Preference serviceLocation = findPreference("weynak.update_location");
                final Preference serviceLocationDistance = findPreference("weynak.distance.interval");
                final Preference serviceLocationTime = findPreference("weynak.time.interval");
                serviceLocation.setEnabled(!isPrivate);
                findPreference("privacycheck").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue)
                    {
                        serviceLocation.setEnabled(!(Boolean) newValue);
                        serviceLocation.setDefaultValue(!(Boolean) newValue);
                        serviceLocationDistance.setEnabled(!(Boolean) newValue);
                        serviceLocationTime.setEnabled(!(Boolean) newValue);
                        return true;
                    }
                });
                
        }
        
    public static void connectToBatteryService(Context context) {	 
    		connectToBatteryService(context,pref.getBoolean("weynak.power_saving.mode",true));
        } 
        
     public static void connectToBatteryService(Context context,Boolean startTheService) {
    		if(startTheService){
    			context.startService(new Intent(context, BatteryService.class));
    		}else{
    			context.stopService(new Intent(context, BatteryService.class));
    		}
        }
}